#include "stack_queue.h"
#include<iostream>
using namespace std;

listNode::listNode(int nodeData,listNode* succ):data(nodeData),next(succ) {}

listNode::~listNode() {}

MyStack::MyStack(): topNode(NULL),stackSize(0) {}

MyStack::~MyStack(){
	listNode* temp = NULL;
	while(topNode != NULL){
		temp = topNode;
		topNode = topNode -> next;
		delete temp;
	}
	stackSize = 0;
}

bool MyStack::push(const int& x){
	listNode* temp = new listNode(x,topNode);
	if(temp == NULL) return false;
	topNode = temp;
	stackSize++;
	return true;
}

bool MyStack::pop(int& item){
	if(topNode -> next == NULL){
		return false;
	}else{
		item = topNode -> data;
		listNode* temp = new listNode(item);
		temp = topNode;
		topNode = topNode -> next;
		delete temp;
		stackSize--;
		return true;
	}
}

bool MyStack::getTop(int& item){
	if(topNode -> next == NULL){
		return false;
	}else{
		item = topNode -> data;
		return true; 
	} 
}

void MyStack::printStack(){
    listNode* p = topNode;

    while(p != NULL){
        if(p->next == NULL)
            cout << p->data << endl;
        else
            cout << p->data << ",";

        p = p->next;
        
    }
}

void MyQueue::printQueue(){
    int cursor = front;

    while(cursor != rear)
    {
        if((cursor + 1) % capacity == rear)
            cout << queue[cursor] << endl;
        else
            cout << queue[cursor] << ",";

        cursor = (cursor + 1) % capacity;
    }
}



